﻿using System;
using System.Text;

namespace JinianNet.Common
{
    /// <summary>
    /// 随机 翅膀的初衷 2011-5-30
    /// </summary>
    public class Random
    {
        /// <summary>
        /// 随机种子
        /// </summary>
        private static int Seep = 0;
        /// <summary>
        /// 根据时间生成随机数字
        /// </summary>
        /// <returns></returns>
        public static string RandTime(int Length)
        {
            if (Length > 0)
                return string.Format("{0}{1}", DateTime.Now.ToString("yyyyMMddHHmmss"), RandNumber(Length));
            else
                return DateTime.Now.ToString("yyyyMMddHHmmss");
        }
        /// <summary>
        /// 生成随机数字
        /// </summary>
        /// <param name="Length">生成长度</param>
        /// <returns></returns>
        public static string RandNumber(int Length)
        {
            StringBuilder result = new StringBuilder();
            SeepChange();
            System.Random random = new System.Random(~unchecked((int)DateTime.Now.Ticks + Seep));
            for (int i = 0; i < Length; i++)
            {
                result.Append(random.Next(10));
            }
            return result.ToString();
        }

        /// <summary>
        /// 获取一个随机概率结果
        /// </summary>
        /// <param name="min">分子</param>
        /// <param name="max">分母</param>
        /// <returns></returns>
        public static bool RandProbability(int min, int max)
        {
            SeepChange();
            System.Random random = new System.Random(~unchecked((int)DateTime.Now.Ticks + Seep));
            return random.Next(1, max + 1) <= min;
        }

        /// <summary>
        /// 获取一个随机概率结果
        /// </summary>
        /// <param name="value">概率</param>
        /// <returns></returns>
        public static bool RandProbability(double value)
        {
            if (value >= 1)
                return true;
            if (value <= 0)
                return false;
            string num = value.ToString().TrimEnd('0');
            int i = num.IndexOf('.');
            int n = 0;
            if (i >= 0)
            {
                n = num.Length - i - 1;
            }

            int max, min;
            if (n > 8)
            {
                n = 8;
            }
            max = int.Parse("1".PadRight(n + 1, '0'));
            min = Convert.ToInt32(Math.Round(value, n) * max);

            return RandProbability(min, max);
        }

        /// <summary>
        /// 生成随机字母与数字
        /// </summary>
        /// <param name="Length">生成长度</param>
        /// <returns></returns>
        public static string RandStr(int Length)
        {
            SeepChange();
            char[] original = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
            StringBuilder result = new StringBuilder();
            int n = original.Length;
            System.Random random = new System.Random(~unchecked((int)DateTime.Now.Ticks + Seep));
            for (int i = 0; i < Length; i++)
            {
                int rnd = random.Next(0, n);
                result.Append(original[rnd]);
            }
            return result.ToString();
        }

        /// <summary>
        /// 生成随机纯字母随机数
        /// </summary>
        /// <param name="Length">生成长度</param>
        /// <returns></returns>
        public static string RandAlphabet(int Length)
        {
            SeepChange();
            char[] original = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
            StringBuilder result = new StringBuilder();
            int n = original.Length;
            System.Random random = new System.Random(~unchecked((int)DateTime.Now.Ticks + Seep));
            for (int i = 0; i < Length; i++)
            {
                int rnd = random.Next(0, n);
                result.Append(original[rnd]);
            }
            return result.ToString();
        }
        /// <summary>
        /// 生成随机汉字
        /// </summary>
        /// <param name="Length">生成长度</param>
        /// <returns></returns>
        public static string RandCharacter(int Length)
        {
            SeepChange();
            //498个高频字
            string original = "的一是在不了有和人这中大为上个国我以要他时来用们生到作地于出就分对成会可主发年动同工也能下过子说产种面而方后多定行学法所民得经十三之进着等部度家电力里如水化高自二理起小物现实加量都两体制机当使点从业本去把性好应开它合还因由其些然前外天政四日那社义事平形相全表间样与关各重新线内数正心反你明看原又么利比或但质气第向道命此变条只没结解问意建月公无系军很情者最立代想已通并提直题党程展五果料象员革位入常文总次品式活设及管特件长求老头基资边流路级少图山统接知较将组见计别她手角期根论运农指几九区强放决西被干做必战先回则任取据处队南给色光门即保治北造百规热领七海口东导器压志世金增争济阶油思术极交受联什认六共权收证改清己美再采转更单风切打白教速花带安场身车例真务具万每目至达走积示议声报斗完类八离华名确才科张信马节话米整空元况今集温传土许步群广石记需段研界拉林律叫且究观越织装影算低持音众书布复容儿须际商非验连断深难近矿千周委素技备半办青省列习响约支般史感劳便团往酸历市克何除消构府称太准精值号率族维划选标写存候毛亲快效斯院查江型眼王按格养易置派层片始却专状育厂京识适属圆包火住调满县局照参红细引听该铁价严";
            StringBuilder result = new StringBuilder();
            int n = original.Length;
            System.Random random = new System.Random(~unchecked((int)DateTime.Now.Ticks + Seep));
            for (int i = 0; i < Length; i++)
            {
                int rnd = random.Next(0, n);
                result.Append(original[rnd]);
            }
            return result.ToString();
        }

        /// <summary>
        /// 随机计算公式
        /// </summary>
        /// <param name="ParametersLength1">参数1长度(最好不超过4位，且比参数2多一位)</param>
        /// <param name="ParametersLength2">参数2长度(最好不超过4位)</param>
        /// <param name="Result">计算结果</param>
        /// <returns>计算公式</returns>
        public static string RandCalculation(int ParametersLength1, int ParametersLength2, out int Result)
        {

            int n1 = Convert.ToInt32(RandNumber(ParametersLength1));
            int n2 = Convert.ToInt32(RandNumber(ParametersLength2));
            SeepChange();
            System.Random random = new System.Random(Seep);
            int symbol = random.Next(0, 4);
            switch (symbol)
            {
                case 0:
                default:
                    Result = n1 + n2;
                    return string.Format("{0} ＋ {1} =", n1, n2);
                case 1:
                    Result = n1 - n2;
                    return string.Format("{0} － {1} =", n1, n2);
                case 2:
                    Result = n1 * n2;
                    return string.Format("{0} × {1} =", n1, n2);
                case 3:
                    if (n2 == 0)
                        n2 = 1;
                    Result = Convert.ToInt32(RandNumber(1));
                    n1 = n2 * Result;
                    return string.Format("{0} ÷ {1} =", n1, n2);
            }
        }
        /// <summary>
        /// 种子变幻
        /// </summary>
        private static void SeepChange()
        {
            if (Seep == int.MaxValue)
                Seep = 0;
            else
                Seep++;
        }
    }
}
